﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace Catering.Catering_Page.Caipin.Caipin_jcsz
{
    public partial class YingYangbiaozhun : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.btn_Add.Enabled = false;
                this.btn_Edit.Enabled = false;
                this.LoadData();
            }
        }

        //加载数据
        public void LoadData()
        {
            DataTable dt_Sex = new Catering_Rules.Sex().GetAllList().Tables[0];
            this.Drop_Sex.LoadDrop(dt_Sex, "id", "SexName");

            DataTable dt_OldArea = new Catering_Rules.OldArea().GetAllList().Tables[0];
            this.Drop_Age.LoadDrop(dt_OldArea, "id", "Area");

            DataTable dt_LaborGrade = new Catering_Rules.LaborGrade().GetAllList().Tables[0];
            this.Drop_laodong.LoadDrop(dt_LaborGrade, "id", "LGName");

            DataTable dt_Group_tedian = new Catering_Rules.Group_tedian().GetAllList().Tables[0];
            this.Drop_shiyong.LoadDrop(dt_Group_tedian, "id", "PName");
        }

        //通过条件查询
        protected void btn_Search_Click(object sender, EventArgs e)
        {
            string SexId = this.Drop_Sex.SelectedValue;
            string AgeId = this.Drop_Age.SelectedValue;
            string LaborGradeId = this.Drop_laodong.SelectedValue;
            string Group_tedianId = this.Drop_shiyong.SelectedValue;
            string strwhere = "SexID=" + SexId + " and OldAreaID=" + AgeId + " and LaborGradeID=" + LaborGradeId + " and Group_tedianID=" + Group_tedianId;

            DataTable dt_Ingestion = new Catering_Rules.Ingestion().GetList(strwhere).Tables[0];
            if (dt_Ingestion != null && dt_Ingestion.Rows.Count > 0)
            {
                foreach (DataRow row in dt_Ingestion.Rows)
                {
                    foreach (Control c in this.yuansu_panel.Controls)
                    {
                        TextBox tb = c as TextBox;
                        if (tb != null && tb.ID.ToString().Replace("txt_", "") == row["NutritionElementsID"].ToString())
                        {
                            tb.Text = row["Number"].ToString();
                        }
                    }
                }
                this.btn_Add.Enabled = false;
                this.btn_Edit.Enabled = true;
            }
            else
            {
                this.txt_Clear();
                this.btn_Add.Enabled = true;
                this.btn_Edit.Enabled = false;
            }
        }

        //清空控件值
        public void txt_Clear()
        {
            foreach (Control c in this.yuansu_panel.Controls)
            {
                TextBox tb = c as TextBox;
                if (tb != null)
                {
                    tb.Text = string .Empty;
                }
            }
        }

        //添加元素标准
        protected void btn_Add_Click(object sender, EventArgs e)
        {
            string SexId = this.Drop_Sex.SelectedValue;
            string AgeId = this.Drop_Age.SelectedValue;
            string LaborGradeId = this.Drop_laodong.SelectedValue;
            string Group_tedianId = this.Drop_shiyong.SelectedValue;
            foreach (Control c in this.yuansu_panel.Controls)
            {
                TextBox tb = c as TextBox;
                if (tb != null)
                {
                    Catering_Model.Ingestion md = new Catering_Model.Ingestion();
                    md.SexID = Convert.ToInt32(SexId);
                    md.OldAreaID = Convert.ToInt32(AgeId);
                    md.LaborGradeID = Convert.ToInt32(LaborGradeId);
                    md.Group_tedianID = Convert.ToInt32(Group_tedianId);
                    md.id = new Catering_Rules.Ingestion().GetMaxId();
                    md.NutritionElementsID = Convert.ToInt32(tb.ID.ToString().Replace("txt_",""));
                    md.Number = tb.Text.Trim() == "" ? "0" : tb.Text.Trim();
                    new Catering_Rules.Ingestion().Add(md);
                }
            }
            this.btn_Add.Enabled = false;
            this.btn_Edit.Enabled = true;
            Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('保存成功！')</script>");
        }

        //修改元素标准
        protected void btn_Edit_Click(object sender, EventArgs e)
        {
            string SexId = this.Drop_Sex.SelectedValue;
            string AgeId = this.Drop_Age.SelectedValue;
            string LaborGradeId = this.Drop_laodong.SelectedValue;
            string Group_tedianId = this.Drop_shiyong.SelectedValue;
            string strwhere = "SexID=" + SexId + " and OldAreaID=" + AgeId + " and LaborGradeID=" + LaborGradeId + " and Group_tedianID=" + Group_tedianId;

            new Catering_Rules.Ingestion().DeleteListBywhere(strwhere);
            foreach (Control c in this.yuansu_panel.Controls)
            {
                TextBox tb = c as TextBox;
                if (tb != null)
                {
                    Catering_Model.Ingestion md = new Catering_Model.Ingestion();
                    md.SexID = Convert.ToInt32(SexId);
                    md.OldAreaID = Convert.ToInt32(AgeId);
                    md.LaborGradeID = Convert.ToInt32(LaborGradeId);
                    md.Group_tedianID = Convert.ToInt32(Group_tedianId);
                    md.id = new Catering_Rules.Ingestion().GetMaxId();
                    md.NutritionElementsID = Convert.ToInt32(tb.ID.ToString().Replace("txt_", ""));
                    md.Number = tb.Text.Trim() == "" ? "0" : tb.Text.Trim();
                    new Catering_Rules.Ingestion().Add(md);
                }
            }
            this.btn_Add.Enabled = false;
            this.btn_Edit.Enabled = true;
            Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('保存成功！')</script>");
        }




    }
}